//
//  ViewController.m
//  MobileNoProject
//
//  Created by apple on 2020/12/17.
//

#import "ViewController.h"
#import <AdSupport/AdSupport.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *noLbl;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //87B2EF5E-BC1C-40F9-BA2B-521E4BF637CA 测试机
    //8DD6489A-DB31-4FE5-9BF4-9B24982B44C1  模拟器
    NSLog(@"UIDevice1:%@",[[[UIDevice currentDevice] identifierForVendor] UUIDString]);//同一个供应商如果com.chidou.xx1 com.chidou.xx2
    //46FA8155-0060-4EE4-A6C8-645FE5559F7F 测试机
    //7EE7BB10-C120-43A0-8240-CCAF8BD65669  模拟器
    NSLog(@"UIDevice3:%@",[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]);
    
    // Do any additional setup after loading the view.
}
- (IBAction)onclick_update {
    //1127840C-CCEA-47B8-BA2A-41FB2035455D
    NSString *adId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSString *strKey = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    id valueStr = [self load:strKey];//A46FA7D0-3B53-413A-A7CE-599FD4FC4AF5
    self.noLbl.text = [NSString stringWithFormat:@"新UUID:\n%@\n\n\n缓存UUID:\n%@",adId,valueStr];
    NSLog(@"\n新UUID:\n%@\n缓存UUID:\n%@",adId,valueStr);
    
    if ([valueStr isKindOfClass:[NSString class]] && ((NSString *)valueStr).length) {
        NSLog(@"已缓存");
    }
    else {
        [self save:strKey data:adId];
        NSLog(@"保存:===%@",adId);
    }
}
//存
- (void)save:(NSString *)service data:(id)data {
    //Get search dictionary
    NSMutableDictionary *keychainQuery = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                          (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
                                          service, (__bridge id)kSecAttrService,
                                          service, (__bridge id)kSecAttrAccount,
                                          (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
                                          nil];
    //Delete old item before add new item
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    //Add new object to search dictionary(Attention:the data format)
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
    //Add item to keychain with the search dictionary
    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
}

//取
- (id)load:(NSString *)service {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                          (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
                                          service, (__bridge id)kSecAttrService,
                                          service, (__bridge id)kSecAttrAccount,
                                          (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
                                          nil];
    //Configure the search setting
    //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
    [keychainQuery setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
        } @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        } @finally {
        }
    }
    if (keyData)
        CFRelease(keyData);
    return ret;
}
@end
